<?php
class Advert_model extends CI_Model
{
    private $table   = 'advert';
    private $table_2 = 'user';
    public function total($params=array()) 
    {
        if (!empty($params['title'])) {
            $this->db->like('title', $params['title']);
        }
        if (!empty($params['position'])) {
            $this->db->where('position', $params['position']);
        }
        if (isset($params['status']) && is_numeric($params['status']) && in_array($params['status'], [0,1])) {
            $this->db->where('status', $params['status']);
        }
        return $this->db->count_all_results($this->table);
    }

    public function page_list($page_num, $num, $params=array())
    {
        $this->db->select('a.*,b.alias_name');
        $this->db->from($this->table.' as a');
        $this->db->join($this->table_2.' as b','a.provider_id = b.uid','left');
        if (!empty($params['title'])) {
            $this->db->like('a.title', $params['title']);
        }
        if (!empty($params['position'])) {
            $this->db->where('a.position', $params['position']);
        }
        if (isset($params['status']) && is_numeric($params['status']) && in_array($params['status'], [0,1])) {
            $this->db->where('a.status', $params['status']);
        }
        $this->db->order_by('a.id','DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    public function insertIntoAdvert($postData)
    {
        $data = array(
            'title'         => trim($postData['title']),
            'position'      => $postData['position'],
            'opentype'      => $postData['opentype'],
            'openvar'       => trim($postData['openvar']),
            'sort'        => $postData['sort'],
            'picture'       => $postData['picture'],
            'provider_id'   => $postData['provider_id']
        );
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['update_time'] = date('Y-m-d H:i:s');
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function updateInfoAdvert($postData)
    {
        $data = array(
            'title'       => trim($postData['title']),
            'openvar'     => trim($postData['openvar']),
            'picture'     => $postData['picture'],
            'position'    => $postData['position'],
            'sort'        => $postData['sort'],
            'opentype'    => $postData['opentype'],
            'provider_id'    => $postData['provider_id'],
        );
        $data['update_time'] = date('Y-m-d H:i:s');
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->table, $data);
    }
    
    public function updateStatus($param){
        $this->db->where('id',$param['id']);
        return $this->db->update($this->table,['status'=>$param['status']]);
    }
    public function deleteById($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }
    
    public function findById($id)
    {
        $this->db->where('id', $id);
        return $this->db->get($this->table);
    }
}